package com.example.fund.fundtrade;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fund.BaseActivity;
import com.example.fund.MainActivity;
import com.example.fund.R;
import com.example.fund.TextWatcherAdapter;
import com.example.fund.dialogFragment.DialogFragmentEx;
import com.example.fund.info.FundInfo;
import com.example.fund.util.DatabaseHelper;
import com.example.fund.util.FundTradeUtil;
import com.example.fund.util.FundUtil;
import com.example.fund.util.FundUtil1;
import com.example.fund.util.StringUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

public class FundTradeDialog extends DialogFragment {

    private SQLiteDatabase db;
    private LinearLayout root;
    private BaseActivity parentActivity;
    private int bg1 = Color.rgb(5, 167, 247);
    private int bg2 = Color.rgb(233,244,251);
    private FundTradeInfo info = new FundTradeInfo();
    //自定义弹出选择框
    private PopupWindow popupWindow;
    private ListView popupList;
    //当前点击的TextView
    private TextView tv;
    //账户选择按钮
    private TextView txtSelectAccount;
    //账户
    private EditText txtAccount;
    private List<String> accountList;
    //基金公司
    private TextView txtCompany;
    private String[] companyList;
    //基金
    private TextView txtFund;
    private TextView txtFundNumber;
    private List<String> fundList;
    //转入基金
    private RelativeLayout layoutTargetFund;
    private TextView txtTargetFund;
    private TextView txtTargetFundNumber;
    //交易类型
    private TextView txtTradeType;
    private TextView txtTradeTypeNumber;
    private List<String> tradeTypeList;
    //交易日期
    private TextView txtTradeDate;
    private List<String> tradeDateList;
    private List<String> tempTradeDateList;
    private TextView txtTradeYear;
    private List<String> tradeYearList;
    //成交价格
    private TextView txtTradePrice;
    //成交金额
    private TextView txtTradeAmount;
    //交易费率%
    private TextView txtTradeRate;
    //成交份额
    private TextView txtTradeQty;
    //全部
    private TextView txtQty1;
    //1/2
    private TextView txtQty2;
    //1/3
    private TextView txtQty3;
    //1/4
    private TextView txtQty4;
    //1/5
    private TextView txtQty5;
    //确定
    private TextView btnConfirm;
    //删除
    private TextView btnDelete;

    public FundTradeDialog() {
        super();
    }

    public void setParentActivity(BaseActivity parentActivity)
    {
        this.parentActivity = parentActivity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.fundtrade_dialog, null);
        this.setCancelable(false);// 设置点击屏幕Dialog不消失
        DatabaseHelper helper = new DatabaseHelper(getActivity());
        db = helper.getWritableDatabase();
        fillInitData();// 接收关联Activity传来的数据
        init(view);
        initListener();
        fillInfoToUI();//填充数据到UI
        return view;
    }

    public void  init(View view)
    {
        root = (LinearLayout)view.findViewById(R.id.root);
        root.setBackgroundColor(bg2);
        //初始化自定义下拉框
        View contentView = LayoutInflater.from(view.getContext()).inflate(R.layout.popupwindow, null);
        contentView.setBackgroundColor(bg2);
        popupWindow = new PopupWindow(root, 400, 900);
        popupWindow.setContentView(contentView);
        popupList = (ListView) contentView.findViewById(R.id.popupList);
        popupList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
        //获取界面的控件
        txtSelectAccount = (TextView)view.findViewById(R.id.txtSelectAccount);
        txtAccount = (EditText)view.findViewById(R.id.txtAccount);
        txtCompany = (TextView)view.findViewById(R.id.txtCompany);
        txtFund = (TextView)view.findViewById(R.id.txtFund);
        txtFundNumber = (TextView)view.findViewById(R.id.txtFundNumber);
        layoutTargetFund = (RelativeLayout)view.findViewById(R.id.layoutTargetFund);
        txtTargetFund = (TextView)view.findViewById(R.id.txtTargetFund);
        txtTargetFundNumber = (TextView)view.findViewById(R.id.txtTargetFundNumber);
        txtTradeType = (TextView)view.findViewById(R.id.txtTradeType);
        txtTradeTypeNumber = (TextView)view.findViewById(R.id.txtTradeTypeNumber);
        txtTradeDate = (TextView)view.findViewById(R.id.txtTradeDate);
        txtTradeYear = (TextView)view.findViewById(R.id.txtTradeYear);
        txtTradePrice = (TextView)view.findViewById(R.id.txtTradePrice);
        txtTradeAmount = (TextView)view.findViewById(R.id.txtTradeAmount);
        txtTradeRate = (TextView)view.findViewById(R.id.txtTradeRate);
        txtTradeQty = (TextView)view.findViewById(R.id.txtTradeQty);
        txtQty1 = (TextView)view.findViewById(R.id.txtQty1);
        txtQty2 = (TextView)view.findViewById(R.id.txtQty2);
        txtQty3 = (TextView)view.findViewById(R.id.txtQty3);
        txtQty4 = (TextView)view.findViewById(R.id.txtQty4);
        txtQty5 = (TextView)view.findViewById(R.id.txtQty5);
        btnConfirm = (TextView)view.findViewById(R.id.btnConfirm);
        btnDelete = (TextView)view.findViewById(R.id.btnDelete);
        if (info.getId()==0)
        {
            btnDelete.setVisibility(View.INVISIBLE);
        }

        accountList = FundTradeUtil.getFundAccountList();
        companyList = FundUtil.companyNames;
        fundList = FundUtil.getAllFundName("");
        tradeTypeList = new LinkedList<String>();
        tradeTypeList.add("1-申购");
        tradeTypeList.add("2-赎回");
        tradeTypeList.add("3-转换");
        tradeDateList = FundUtil.getSHIndexDateList();
        tempTradeDateList = new LinkedList<String>();
        tradeYearList = new LinkedList<String>();
        Calendar c = Calendar.getInstance();
        for (int i=0;i<20;i++)
        {
            int year = c.get(Calendar.YEAR);
            tradeYearList.add(year+"");
            c.add(Calendar.YEAR,-1);
        }
    }

    /**
     * 注册事件
     */
    public void initListener()
    {
        //账户
        txtSelectAccount.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                initPopWindow(txtAccount,accountList.toArray());
            }
        });
        //基金公司
        txtCompany.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                initPopWindow((TextView) arg0,companyList);
            }
        });
        //基金
        txtFund.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                String company = txtCompany.getText().toString();
                if (!StringUtil.isNullOrEmpty(company) && !"基金".equals(company))
                {
                    fundList = FundUtil.getAllFundName(company);
                }else {
                    fundList = FundUtil.getAllFundName("");
                }

                initPopWindow((TextView) arg0,fundList.toArray());
            }
        });
        //选择基金后事件
        txtFund.addTextChangedListener(new TextWatcherAdapter()
        {
            @Override
            public void afterTextChanged(Editable arg0) {
                String fundNumber = FundUtil.getFundNumber(txtFund.getText().toString());
                txtFundNumber.setText(fundNumber);
                super.afterTextChanged(arg0);
            }
        });
        //转入基金
        txtTargetFund.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                String company = txtCompany.getText().toString();
                if (!StringUtil.isNullOrEmpty(company) && !"基金".equals(company))
                {
                    fundList = FundUtil.getAllFundName(company);
                }else {
                    fundList = FundUtil.getAllFundName("");
                }

                initPopWindow((TextView) arg0,fundList.toArray());
            }
        });
        //选择转入基金后事件
        txtTargetFund.addTextChangedListener(new TextWatcherAdapter()
        {
            @Override
            public void afterTextChanged(Editable arg0) {
                String fundNumber = FundUtil.getFundNumber(txtTargetFund.getText().toString());
                txtTargetFundNumber.setText(fundNumber);
                super.afterTextChanged(arg0);
            }
        });
        //基金类型
        txtTradeType.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                initPopWindow((TextView) arg0,tradeTypeList.toArray());
            }
        });
        //选择交易类型后事件
        txtTradeType.addTextChangedListener(new TextWatcherAdapter()
        {
            @Override
            public void afterTextChanged(Editable arg0) {
                String tradeType = txtTradeType.getText().toString();
                if(StringUtil.isNullOrEmpty(tradeType))
                {
                    txtTradeTypeNumber.setText("0");
                }else
                {
                    String[] typeArr = tradeType.split("-");
                    if (typeArr.length==2)
                    {
                        txtTradeTypeNumber.setText(typeArr[0]);
                    }else
                    {
                        txtTradeTypeNumber.setText("0");
                    }
                }
                String tradeTypeNumber = txtTradeTypeNumber.getText().toString();
                if ("3".equals(tradeTypeNumber))
                {
                    layoutTargetFund.setVisibility(View.VISIBLE);
                }else
                {
                    layoutTargetFund.setVisibility(View.GONE);
                }
                super.afterTextChanged(arg0);
            }
        });

        //交易日期
        txtTradeDate.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                String year = txtTradeYear.getText().toString();
                tempTradeDateList.clear();
                for(int i=0,s=tradeDateList.size();i<s;i++)
                {
                    String deteStr = tradeDateList.get(i);
                    if (deteStr.startsWith(year))
                    {
                        tempTradeDateList.add(deteStr);
                    }
                }
                initPopWindow((TextView) arg0,tempTradeDateList.toArray());
            }
        });
        txtTradeYear.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                initPopWindow((TextView) arg0,tradeYearList.toArray());
            }
        });
        //下拉框选中事件
        popupList.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
            {
                tv.setText(((TextView)arg1).getText());
                popupWindow.dismiss();
            }
        });

        // 按返回时关闭窗口
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    FundTradeDialog.this.dismiss();
                    return true;
                }
                return false;
            }
        });
        //点击确定保存数据
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnConfirm_onClick(v);
            }
        });
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnDelete_onClick(v);
            }
        });

        //值改变，计算金额或份额
        TextWatcher textWatcher = new TextWatcherAdapter(){
            @Override
            public void afterTextChanged(Editable arg0) {
             //   calFundTradeData(arg0);
                super.afterTextChanged(arg0);
            }
        };
        txtFundNumber.addTextChangedListener(new TextWatcherAdapter(){
            @Override
            public void afterTextChanged(Editable arg0) {
                calFundTradeData(txtFundNumber);
                super.afterTextChanged(arg0);
            }
        });
        txtTradeDate.addTextChangedListener(new TextWatcherAdapter(){
            @Override
            public void afterTextChanged(Editable arg0) {
                calFundTradeData(txtTradeDate);
                super.afterTextChanged(arg0);
            }
        });
        txtTradeAmount.addTextChangedListener(new TextWatcherAdapter(){
            @Override
            public void afterTextChanged(Editable arg0) {
                calFundTradeData(txtTradeAmount);
                super.afterTextChanged(arg0);
            }
        });
        txtTradeRate.addTextChangedListener(new TextWatcherAdapter(){
            @Override
            public void afterTextChanged(Editable arg0) {
                calFundTradeData(txtTradeRate);
                super.afterTextChanged(arg0);
            }
        });
        txtTradeQty.addTextChangedListener(new TextWatcherAdapter(){
            @Override
            public void afterTextChanged(Editable arg0) {
                calFundTradeData(txtTradeQty);
                super.afterTextChanged(arg0);
            }
        });


        //预定义份额点击事件
        View.OnClickListener qtyOnClickListener = new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                txtQty_onClick(arg0);
            }
        };
        txtQty1.setOnClickListener(qtyOnClickListener);
        txtQty2.setOnClickListener(qtyOnClickListener);
        txtQty3.setOnClickListener(qtyOnClickListener);
        txtQty4.setOnClickListener(qtyOnClickListener);
        txtQty5.setOnClickListener(qtyOnClickListener);
    }

    /**
     * 接收关联Activity传来的数据
     */
    public void fillInitData()
    {
        // 接收关联Activity传来的数据
        Bundle bundle = getArguments();

        if (bundle != null) {
            info.setId(bundle.getInt("id"));
            info.setAccount(bundle.getString("account"));
            info.setFundName(bundle.getString("fundName"));
            info.setFundNumber(bundle.getString("fundNumber"));
            info.setDate(bundle.getString("tradeDate"));
            info.setType(bundle.getInt("tradeType"));
            info.setTradePrice(bundle.getDouble("tradePrice"));
            info.setTradeAmount(bundle.getDouble("tradeAmount"));
            info.setTradeRate(bundle.getDouble("tradeRate"));
            info.setTradeQty(bundle.getDouble("tradeQty"));
        }
    }

    /**
     * 填充数据到UI
     */
    private void fillInfoToUI()
    {
        if (info==null)
        {
            return;
        }

        txtAccount.setText(info.getAccount());
        txtFund.setText(info.getFundName());
        txtFundNumber.setText(info.getFundNumber());
        String tradeType = "交易类型";
        if (info.getType()==1)
        {
            tradeType = "1-申购";
        }else if (info.getType()==2)
        {
            tradeType = "2-赎回";
        }
        txtTradeType.setText(tradeType);
        txtTradeTypeNumber.setText(String.valueOf(info.getType()));
        txtTradeDate.setText(info.getDate());
        txtTradePrice.setText(String.valueOf(info.getTradePrice()));
        txtTradeAmount.setText(String.valueOf(info.getTradeAmount()));
        txtTradeRate.setText(String.valueOf(info.getTradeRate()));
        txtTradeQty.setText(String.valueOf(info.getTradeQty()));
    }

    /**
     * 点击确定保存数据
     * @param v
     */
    private void btnConfirm_onClick(View v)
    {
        fillUIDataToInfo();//获取界面数据填充到Info

        if (StringUtil.isNullOrEmpty(info.getAccount()))
        {
            Toast.makeText(this.getActivity(), "请选择或新建账户", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtil.isNullOrEmpty(info.getFundNumber()))
        {
            Toast.makeText(this.getActivity(), "请选择基金", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtil.isNullOrEmpty(info.getDate()))
        {
            Toast.makeText(this.getActivity(), "请选择交易日期", Toast.LENGTH_SHORT).show();
            return;
        }
        if (info.getType()!=1 && info.getType()!=2 && info.getType()!=3)
        {
            Toast.makeText(this.getActivity(), "请选择交易类型", Toast.LENGTH_SHORT).show();
            return;
        }
        if (info.getTradePrice()<=0)
        {
            Toast.makeText(this.getActivity(), "成交价格必须大于0", Toast.LENGTH_SHORT).show();
            return;
        }
        if (info.getTradeAmount()<=0)
        {
            Toast.makeText(this.getActivity(), "成交金额必须大于0", Toast.LENGTH_SHORT).show();
            return;
        }
        if (info.getTradeQty()<=0)
        {
            Toast.makeText(this.getActivity(), "交易份额必须大于0", Toast.LENGTH_SHORT).show();
            return;
        }
        if (info.getTradeRate()<0 && info.getTradeRate()>1)
        {
            Toast.makeText(this.getActivity(), "交易费率必须在0和1之间", Toast.LENGTH_SHORT).show();
            return;
        }
        if (info.getType()==3)
        {
            if (StringUtil.isNullOrEmpty(info.getTargetFundNumber()))
            {
                Toast.makeText(this.getActivity(), "请选择转入基金", Toast.LENGTH_SHORT).show();
                return;
            }
            double netValue = FundUtil.getFundNetValue(info.getTargetFundNumber(),info.getDate());
            if (netValue==0)
            {
                Toast.makeText(this.getActivity(), "转入基金"+info.getDate()+"的净值为空", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        boolean flag = FundTradeUtil.saveFundTradeInfo(info);
        if (flag)
        {
            if (parentActivity instanceof FundTradeList)
            {
                FundTradeList fundTradeList = (FundTradeList)parentActivity;
                fundTradeList.txtQuery_OnClickListener();
            }
            this.dismiss();
        }else
        {
            Toast.makeText(this.getActivity(), "保存失败", Toast.LENGTH_SHORT).show();
            return;
        }
    }

    /**
     * 删除
     * @param v
     */
    private void btnDelete_onClick(View v)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
        builder.setTitle("删除");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setMessage("确定删除该交易记录?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                FundTradeUtil.deleteFundTradeInfo(info.getId());
                Toast.makeText(FundTradeDialog.this.getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
                if (parentActivity instanceof FundTradeList)
                {
                    FundTradeList fundTradeList = (FundTradeList)parentActivity;
                    fundTradeList.txtQuery_OnClickListener();
                }
                FundTradeDialog.this.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    /**
     * 获取界面数据填充到Info
     */
    private void fillUIDataToInfo()
    {
        info.setAccount(txtAccount.getText().toString());
        info.setFundName(txtFund.getText().toString());
        info.setFundNumber(txtFundNumber.getText().toString());
        info.setTargetFundName(txtTargetFund.getText().toString());
        info.setTargetFundNumber(txtTargetFundNumber.getText().toString());
        info.setDate(txtTradeDate.getText().toString());
        String tradeTypeNumber = txtTradeTypeNumber.getText().toString();
        if (StringUtil.isNullOrEmpty(tradeTypeNumber))
        {
            info.setType(0);
        }else
        {
            info.setType(Integer.valueOf(tradeTypeNumber));
        }
        String tradePrice = txtTradePrice.getText().toString();
        if (StringUtil.isNullOrEmpty(tradePrice))
        {
            info.setTradePrice(0);
        }else
        {
            info.setTradePrice(Double.valueOf(tradePrice));
        }
        String tradeAmount = txtTradeAmount.getText().toString();
        if (StringUtil.isNullOrEmpty(tradeAmount))
        {
            info.setTradeAmount(0);
        }else
        {
            info.setTradeAmount(Double.valueOf(tradeAmount));
        }
        String tradeRate = txtTradeRate.getText().toString();
        if (StringUtil.isNullOrEmpty(tradeRate))
        {
            info.setTradeRate(0);
        }else
        {
            info.setTradeRate(Double.valueOf(tradeRate));
        }
        String tradeQty = txtTradeQty.getText().toString();
        if (StringUtil.isNullOrEmpty(tradeQty))
        {
            info.setTradeQty(0);
        }else
        {
            info.setTradeQty(Double.valueOf(tradeQty));
        }
    }

    /**
     * 计算交易数据
     */
    private void calFundTradeData(TextView view)
    {
        String fundNumber = txtFundNumber.getText().toString();
        String date = txtTradeDate.getText().toString();
        if (StringUtil.isNullOrEmpty(fundNumber) || StringUtil.isNullOrEmpty(date))
        {
            txtTradePrice.setText("0.0");
         //   txtTradeQty.setText("0.0");
            return;
        }
        double netValue = FundUtil.getFundNetValue(fundNumber,date);
        if (netValue<=0)
        {
            txtTradePrice.setText("0.0");
          //  txtTradeQty.setText("0.0");
            return;
        }
        txtTradePrice.setText(new BigDecimal(netValue).setScale(4, RoundingMode.HALF_UP).toString());
        String tradeAmountStr = txtTradeAmount.getText().toString();
        String tradeRateStr = txtTradeRate.getText().toString();
        String tradeQtyStr = txtTradeQty.getText().toString();
        if(StringUtil.isNullOrEmpty(tradeAmountStr)
                ||StringUtil.isNullOrEmpty(tradeAmountStr.replaceAll("\\.","").replaceAll("0","")))
        {
            tradeAmountStr = "0";
        }
        if(StringUtil.isNullOrEmpty(tradeRateStr)
                ||StringUtil.isNullOrEmpty(tradeRateStr.replaceAll("\\.","").replaceAll("0","")))
        {
            tradeRateStr = "0";
        }
        if(StringUtil.isNullOrEmpty(tradeQtyStr)
                ||StringUtil.isNullOrEmpty(tradeQtyStr.replaceAll("\\.","").replaceAll("0","")))
        {
            tradeQtyStr = "0";
        }
        double tradeAmount = Double.valueOf(tradeAmountStr);
        double tradeRate = Double.valueOf(tradeRateStr);
        double oldTradeQty = Double.valueOf(tradeQtyStr);
        double tradeQty = tradeAmount*(1-tradeRate/100.00d)/netValue;
        if (Math.abs(tradeQty-oldTradeQty)<=0.0009)
        {
            return;
        }
        if (R.id.txtTradeQty==view.getId())//如果修改的是份额
        {
            tradeAmount = oldTradeQty*netValue/(1-tradeRate/100.00d);
            txtTradeAmount.setText(new BigDecimal(tradeAmount).setScale(6, RoundingMode.HALF_UP).toString());
        }else {
            txtTradeQty.setText(new BigDecimal(tradeQty).setScale(6, RoundingMode.HALF_UP).toString());
        }
    }

    /**
     * 初始化弹出下拉框，
     * 适用范围：时间区间、涨幅、跌幅
     * @param view
     * @param objects
     */
    @SuppressWarnings("unchecked")
    private void initPopWindow(final TextView view,final Object[] objects){
        ArrayAdapter adapter = new ArrayAdapter(view.getContext(), android.R.layout.simple_list_item_1, objects)
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView mTextView = new TextView(view.getContext());
                mTextView.setText(objects[position].toString());
                mTextView.setTextSize(15);
                mTextView.setTextColor(bg1);
                mTextView.setPadding(30, 10, 0, 10);
                return mTextView;
            }
        };
        popupList.setAdapter(adapter);
        String text = view.getText().toString();
        for(int i=0;i<objects.length;i++)
        {
            if(text.equals(objects[i]))
            {
                popupList.setSelection(i);
                break;
            }
        }
        popupWindow.showAsDropDown(view);
        tv = view;
    }

    /**
     * 预定义份额点击事件,改变份额
     * @param view
     */
    private void txtQty_onClick(View view)
    {
        String account = txtAccount.getText().toString();
        String fundNumber = txtFundNumber.getText().toString();
        String date = txtTradeDate.getText().toString();
        if (StringUtil.isNullOrEmpty(account) || StringUtil.isNullOrEmpty(fundNumber) || StringUtil.isNullOrEmpty(date))
        {
            txtTradeQty.setText("0.0");
            return;
        }

        double qty = FundTradeUtil.getTradeQtyTotal(account,fundNumber,date);

        double tradeQty = 0;
        if (txtQty1.getId()==view.getId())
        {
            tradeQty = qty;
        }else if (txtQty2.getId()==view.getId())
        {
            tradeQty = qty/2.0d;
        }else if (txtQty3.getId()==view.getId())
        {
            tradeQty = qty/3.0d;
        }else if (txtQty4.getId()==view.getId())
        {
            tradeQty = qty/4.0d;
        }else if (txtQty5.getId()==view.getId())
        {
            tradeQty = qty/5.0d;
        }

        txtTradeQty.setText(new BigDecimal(tradeQty).setScale(6, RoundingMode.HALF_UP).toString());
    }
}
